package com.cpi.cornpp.purchaseorder.spot.converter;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.cpi.cornpp.common.exception.CornPPException;
import com.cpi.cornpp.purchaseorder.spot.dto.PurchaseOrderSpotDTO;
import com.cpi.cornpp.purchaseorder.spot.exception.PurchaseOrderSpotException;
import com.cpi.cornpp.purchaseorder.spot.view.PurchaseOrderSpotView;
import com.cs.common.utils.NumberUtil;
import com.cs.common.utils.TimeUtils;

/**
 * Purchase Order Spot Converter
 * @author Edgar Joao
 *
 */
public class PurchaseOrderSpotConverter {
	/**
	 * @author Edgar Joao
	 * @param view
	 * @return
	 * @throws PurchaseOrderSpotException
	 */
	public PurchaseOrderSpotView converterPurchaseOrderSpotDtoToView(PurchaseOrderSpotDTO dto) throws PurchaseOrderSpotException{		
		PurchaseOrderSpotView view = null;
		try{
			if(dto != null){
				view = new PurchaseOrderSpotView();
				view.setPurchaseOrderSpotId(dto.getPurchaseOrderSpotId());
				view.setPurchaseOrderId(dto.getPurchaseOrderId());
				view.setQuantity(NumberUtil.convertQuantity(dto.getQuantity()));
				view.setUnitPrice(NumberUtil.convertPrice(dto.getUnitPrice()));
				view.setBushels(NumberUtil.convertQuantity(dto.getBushelUnit()*dto.getQuantity()));
				view.setBushelUnit(dto.getBushelUnit());
				view.setTotalPrice(NumberUtil.convertQuantity(dto.getBushelUnit()*dto.getQuantity()*dto.getUnitPrice()));
				view.setPurchaseDate(TimeUtils.convertSQLDateToString(dto.getPurchaseDate()));
			}
		}catch( Exception exception ){
			PurchaseOrderSpotException purchaseOrderSpotException=null;
			exception.printStackTrace(System.out);
			purchaseOrderSpotException= new PurchaseOrderSpotException(exception , CornPPException.LAYER_CONVERTER,CornPPException.ACTION_LISTS);
			throw purchaseOrderSpotException;
		}
		return view;
	}
	/**
	 * @author Edgar Joao
	 * @param view
	 * @return
	 * @throws PurchaseOrderSpotException
	 */
	public PurchaseOrderSpotDTO converterPurchaseOrderSpotViewToDto(PurchaseOrderSpotView view) throws PurchaseOrderSpotException{
		PurchaseOrderSpotDTO dto = null;
		try{
			if(view != null){
				dto = new PurchaseOrderSpotDTO();
				dto.setPurchaseOrderSpotId(view.getPurchaseOrderSpotId());
				if( view.getPurchaseDate() != null && !view.getPurchaseDate().equals(StringUtils.EMPTY) ){
					dto.setPurchaseDate(TimeUtils.convertStringToSQLDate(view.getPurchaseDate()));
					dto.setQuantity(NumberUtil.parseDouble(view.getQuantity()));
					dto.setUnitPrice(NumberUtil.parseDouble(view.getUnitPrice()));
					dto.setPurchaseOrderId(view.getPurchaseOrderId());
				}
			}
		}catch( Exception exception ){
			PurchaseOrderSpotException purchaseOrderSpotException=null;
			exception.printStackTrace(System.out);
			purchaseOrderSpotException= new PurchaseOrderSpotException(exception , CornPPException.LAYER_CONVERTER,CornPPException.ACTION_LISTS);
			throw purchaseOrderSpotException;
		}
		return dto;
	}
	/**
	 * @author Edgar Joao
	 * @param purchaseOrderSpotDTOs
	 * @return
	 * @throws PurchaseOrderSpotException
	 */
	public List convertDTOsToViews(List purchaseOrderSpotDTOs) throws PurchaseOrderSpotException{
		List list = null;
		Iterator iterator = null;
		PurchaseOrderSpotDTO purchaseOrderSpotDTO = null;
		PurchaseOrderSpotView purchaseOrderSpotView = null;
		try{
			list = new ArrayList(0);
			iterator = purchaseOrderSpotDTOs.iterator();
			while( iterator.hasNext() ){
				purchaseOrderSpotDTO =  (PurchaseOrderSpotDTO) iterator.next();
				purchaseOrderSpotView = converterPurchaseOrderSpotDtoToView(purchaseOrderSpotDTO);
				list.add(purchaseOrderSpotView);
			}						
		}catch(PurchaseOrderSpotException purchaseOrderSpotException){
			throw purchaseOrderSpotException;
		}catch( Exception exception ){
			PurchaseOrderSpotException purchaseOrderSpotException=null;
			exception.printStackTrace(System.out);
			purchaseOrderSpotException= new PurchaseOrderSpotException(exception , CornPPException.LAYER_CONVERTER,CornPPException.ACTION_LISTS);
			throw purchaseOrderSpotException;
		}
		return list;
	}
	
}
